using UnityEngine;

public class WeaponSystem : MonoBehaviour
{

    [SerializeField] private Transform firePoint;
    [SerializeField] private Transform shootTrial;

    private Animator animator;

    private void Awake()
    {
        animator = GetComponent<Animator>();

    }



    // Update is called once per frame
    private void Update()
    {
        animator.SetBool(StringHash.IsShooting, false);
        if (Input.GetButtonDown("Fire1") || Input.GetMouseButtonDown(0))
        {
            animator.SetBool(StringHash.IsShooting, true);
            Debug.Log("射击");


            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 firePointPosition = firePoint.position;
            Vector2 shootDirection = mousePosition - firePointPosition;
            // 当距离过近的时候,射击方向修正
            if (Vector2.Distance(mousePosition, firePointPosition) < 0.5f)
            {
                shootDirection = transform.up;
                //又或者shootDirection = firePoint.parent.up;
            }

            // 子弹发射动画处理
            float trialAngle = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg;
            Quaternion trailRotation = Quaternion.AngleAxis(trialAngle, Vector3.forward);
            Transform trial = Instantiate(shootTrial, firePointPosition, trailRotation);
            Destroy(trial.gameObject, 0.05f);
        }
    }

}
